<template>
  <div>
    <div
      style="font-size: 16px;font-weight: 600;color: #1D2129;margin-bottom: 16px;"
    >
      险种投险数据
    </div>
    <div style="display: flex;width:100%; margin-left: -13%; ">
      <div ref="chart" style="width:50%;height:129px"></div>
      <div
        style="width:50%;height:129px;padding: 2px 10px; display: flex; flex-wrap: wrap;justify-content: space-between;"
      >
        <div style="display: flex;" v-for="(item, index) in namedata">
          <div :style="{ backgroundColor: item.color }" class="dianidan"></div>
          <div>
            <div style="font-size: 12px;margin-right: 7px; width: 61px;">
              {{ item.name }}
            </div>
            <div style="font-size: 22px;color: #0F0F0F;">
              {{ item.value }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { getSheet1InsuranceData2 } from "@/api/statistics";
export default {
  data() {
    return {
      date: new Date().getFullYear(),
      activeTab: 1,
      namedata: [
        { name: "公共责任险", value: 100, color: "#4E88F5" },
        { name: "人身意外伤害险", value: 100, color: "#EEB74A" },
        { name: "其他险种", value: 100, color: "##14C9C9" }
      ]
    };
  },

  mounted() {
    this.datalist();
  },
  methods: {
    async datalist(e) {
      const res = await getSheet1InsuranceData2({
        year: this.date,
      });
      if(res.data){
        this.namedata = res.data
      }
      this.chart = echarts.init(this.$refs.chart);

      // 指定图表的配置项和数据
      const option = {
        title: {
          text: "3326",
          left: "center",
          top: "42%",
          textStyle: {
            color: "#333",
            fontSize: 15,
            align: "center"
          }
        },
        tooltip: {
          trigger: "item"
        },

        legend: {
          top: "5%",
          left: "center",
          show: false
        },
        series: [
          {
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: "center"
            },
            radius: ["65%", "80%"],
            labelLine: {
              show: false
            },
            data: [
              { value: 1048, name: "公共责任险" },
              { value: 735, name: "人身意外伤害险" },
              { value: 735, name: "其他险种" }
            ]
          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      this.chart.setOption(option);

      // 监听父元素大小变化 刷新图表
      var ro = new ResizeObserver(entries => {
        for (let entry of entries) {
          const cr = entry.contentRect;
          this.chart.resize();
        }
      });

      ro.observe(this.$refs.chart);
    }
  }
};
</script>

<style>
.dianidan {
  width: 7px;
  height: 7px;
  margin-right: 5px;
}
</style>
